﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using StaticPlugin;
using System.Deployment.Application;

namespace Bot2
{
    public partial class BotWindow : Form
    {
        public BotWindow()
        {
            InitializeComponent();
            executer = new Executer(browser,jobs);
            styleDefaultRow = dgwJobs.RowsDefaultCellStyle.Clone();
            styleSelectedAction = styleDefaultRow.Clone();
            styleSelectedAction.BackColor = Color.LightGreen;
            styleSelectedAction.SelectionBackColor = Color.LightGreen;
        }

        private void cbActivated_CheckedChanged(object sender, EventArgs e)
        {
            //browserTimer.Enabled = cbActivated.Checked;
        }

        public List<AJob> jobs = new List<AJob>();

        private void Form1_Load(object sender, EventArgs e)
        {
            string ver = Application.ProductVersion + " (not deployed)";
            try
            {
                ver = ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString();
            }
            catch (Exception) { }
            this.Text = "dBot Version " + ver;
            jobs.Clear();
            jobs.AddRange(PluginManager<AJob>.GetList());
            dgwJobs.DataSource = jobs;
        }

        Executer executer;
        //Updater updater = new Updater();
        DataGridViewCellStyle styleDefaultRow;
        DataGridViewCellStyle styleSelectedAction;

        private void browserTimer_Tick(object sender, EventArgs e)
        {
            //updater.TryCheckForUpdates();
            if (!cbActivated.Checked) return;
            int activeJobNum = executer.Ping();
            foreach (DataGridViewRow r in dgwJobs.Rows) r.DefaultCellStyle = styleDefaultRow;
            if (activeJobNum != -1) dgwJobs.Rows[activeJobNum].DefaultCellStyle = styleSelectedAction;
            dgwJobs.DataSource = jobs;
            dgwJobs.Refresh();
        }

        private void dgwJobs_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            AJob j = (AJob)dgwJobs.SelectedRows[0].DataBoundItem;
            Console.WriteLine("job name current is " + j.Name);
            j.showSetupDialog();
            dgwJobs.DataSource = jobs;
            dgwJobs.Refresh();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Properties.Settings.Default.Save();
        }
    }
}
